package longrise.phone.com.bjjt_jyb.View;



import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import longrise.phone.com.bjjt_jyb.R;


public class AlertDialogTips {
	
	private Context context;
	private Dialog dialog;
	private LinearLayout lLayout_bg;
	private TextView txt_msg;
	private Button btn_neg;
	private Button btn_pos;
	private Button btn_confirm;
	private Display display;
	
	//是否只显示一个按钮
	private boolean isSingleConfirm = true;

	public AlertDialogTips(Context context) {
		this.context = context;
		WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		display = windowManager.getDefaultDisplay();
	}

	public AlertDialogTips builder(){
		// 重写Dialog
		View view = LayoutInflater.from(context).inflate(R.layout.alertdialog_tips, null);

		lLayout_bg = (LinearLayout) view.findViewById(R.id.tips_bg);
		
		txt_msg = (TextView) view.findViewById(R.id.tips_txt_msg);
		
		btn_confirm = (Button) view.findViewById(R.id.tips_btn_confirm);
		btn_confirm.setVisibility(View.GONE);
		
		btn_neg = (Button) view.findViewById(R.id.tips_btn_neg);
		btn_neg.setVisibility(View.GONE);
		btn_pos = (Button) view.findViewById(R.id.tips_btn_pos);
		btn_pos.setVisibility(View.GONE);
		
		// Dialog
		dialog = new Dialog(context, R.style.AlertDialogStyle);
		dialog.setContentView(view);

		// dialog
		lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) (display.getWidth() * 0.85), LayoutParams.WRAP_CONTENT));

		return this;
	}

	
	public AlertDialogTips setCancelable(boolean cancel) {
		dialog.setCancelable(cancel);
		return this;
	}

	public AlertDialogTips setPositiveButton(String text,
			final OnClickListener listener) {
		
		Button positiveBtn = null;
		if(isSingleConfirm) {
			positiveBtn = btn_confirm;
		} else {
			positiveBtn = btn_pos;
		}
		
		if ("".equals(text)) {
			positiveBtn.setText("确定");
		} else {
			positiveBtn.setText(text);
		}
		positiveBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				listener.onClick(v);
				dismiss();
			}
		});
		return this;
	}

	public AlertDialogTips setNegativeButton(String text,
			final OnClickListener listener) {
		if(isSingleConfirm) {
			return this ;
		} 
		
		if ("".equals(text)) {
			btn_neg.setText("取消");
		} else {
			btn_neg.setText(text);
		}
		btn_neg.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				listener.onClick(v);
				dismiss();
			}
		});
		return this;
	}
	
	public void dismiss() {
		if(dialog.isShowing()) {					
			dialog.dismiss();
		}
	}
	
	public void show() {
		dialog.show();
	}
	
	public void setContent(String content) {
		txt_msg.setText(content);
	}
	
	public void isSingleConfirm(boolean b) {
		this.isSingleConfirm = b;
		
		if(b) {
			btn_neg.setVisibility(View.GONE);
			btn_pos.setVisibility(View.GONE);
			btn_confirm.setVisibility(View.VISIBLE);
			
		} else {
			btn_neg.setVisibility(View.VISIBLE);
			btn_pos.setVisibility(View.VISIBLE);
			btn_confirm.setVisibility(View.GONE);
		}
	}
	
}
